package com.robotic.kafka;

import com.alibaba.fastjson2.JSONObject;
import com.robotic.route.domain.InspectionRecordDetails;
import com.robotic.route.mapper.InspectionRecordDetailsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class KafkaConsumerService {

    @Autowired
    private InspectionRecordDetailsMapper inspectionRecordDetailsMapper;
    // 主题名称
    private static final String TOPIC_NAME = "robotic-consumer";
    // 消费者组ID
    private static final String GROUP_ID = "sensor-group5";

    /**
     * 监听指定主题的消息。
     * @param message 接收到的消息内容
     */
    @KafkaListener(topics = TOPIC_NAME, groupId = GROUP_ID)
    public void listen(String message) {
        JSONObject jsonObject = JSONObject.parseObject(message, JSONObject.class);
        String deviceName = String.valueOf(jsonObject.get("deviceName"));
        if ("四足机器人".equals(deviceName)) {
            String captureTime = String.valueOf(jsonObject.get("captureTime"));
            Date alarmTime = new Date(Long.parseLong(captureTime));
            String backgroundPicUrl = String.valueOf(jsonObject.get("backgroundPicUrl"));
            String behaviorName = String.valueOf(jsonObject.get("behaviorName"));
            String pointCode = String.valueOf(jsonObject.get("pointCode"));
            InspectionRecordDetails detailNew = inspectionRecordDetailsMapper.getDetailNew(alarmTime, pointCode);
            if (detailNew != null) {
                detailNew.setUrl(backgroundPicUrl);
                detailNew.setAlarmTime(alarmTime);
                detailNew.setAlarmInfo(behaviorName);
                detailNew.setAlarmType("1");
                detailNew.setUpdateTime(new Date());
                inspectionRecordDetailsMapper.updateById(detailNew);
            }
            log.info("接收到消息: = {}", message);
        }
    }



}
